package com.etsmtl.log320.sudo;

public class SudokuSolver {
	
	private Grid grid;
	private SolveStrategy solveStrat;
	
	/**
	 * Constructor
	 * @param gridValue values of the grid in a string, line by line concatenated
	 * (line1 + line2 + line 2 + ... + line 9) Note: zero mean an empty tile
	 * @param strategyNumber enum representing the solving algorithm to use
	 */
	public SudokuSolver(String gridValue, StrategyEnum strategyNumber){
		grid = new Grid(gridValue);
		switch(strategyNumber){
		case Backtrack :
			solveStrat = new BackTrackStrategy();
			break;
		case OptimizedBacktrack:
			solveStrat = new BackTrackOptimisedStrategy();
			break;
		}
	}
	/**
	 * solve the sudoku and print it in the console with the time it took to solve
	 * @return true if the sudoku has been solved and false if its unsolvable
	 */
	public boolean solve(){
		long startTime = System.currentTimeMillis();
		//calling the strategy pattern
		grid.buildValidationTable();
		boolean isSolvable = solveStrat.solve(grid);
		
		System.out.println("Time to solve: " + (System.currentTimeMillis()-startTime) + "ms");
		if(isSolvable){
			System.out.println("Solution:");
			System.out.println(grid.getGridContent());
		}
		else
			System.out.println("There is no solution to this Sudoku");
		
		return isSolvable;
	}	
}
